import React, {Component} from 'react';

import {Button, Spin} from 'antd';
import "../css/roleAssignment.css";
import {EditRoles} from "../components/EditComponent";
import {roleAssignment} from "../value";
import app from '../AppMethod';
import {Consumer} from "../store";

class CustomerManage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            editVisible: false,
            editConfirmLoading: false,
            dutyEmployee1: [],
            dutyEmployee2: [],
            dutyEmployee3: [],
            dutyEmployee4: [],
            dutyEmployee5: [],
            employeeTypeItem: [
                {
                    type_name: '接单负责人',
                    dutyEmployee: []
                },
                {
                    type_name: '配货负责人',
                    dutyEmployee: []
                },
                {
                    type_name: '对单负责人',
                    dutyEmployee: []
                },
                {
                    type_name: '配送负责人',
                    dutyEmployee: []
                },
                {
                    type_name: '入库负责人',
                    dutyEmployee: []
                }
            ]
        }
    }

    render() {
        return (
            <div>
                <div style={{paddingLeft: '35px'}}><Button type='primary' onClick={() => {
                    this.handleRefresh()
                }}>刷新</Button></div>
                <div className="member_box clearfix">
                    {
                        this.state.employeeTypeItem.map((item, index) => {
                            return (
                                <div className="member_list" key={index}>
                                    <div className="member_title">{item.type_name}<EditRoles
                                        handleGetEmployee={() => this.handleGetEmployee()} type={index}
                                        data={item.dutyEmployee}/></div>
                                    {
                                        item.dutyEmployee.map((item, subIndex) => {
                                            return <div className="member_name"
                                                        key={index + subIndex}>{item.employee_name}</div>
                                        })
                                    }
                                </div>
                            )
                        })
                    }
                </div>
            </div>
        )
    };

    componentDidMount() {
        const {dispatch} = this.props;
        dispatch({menuType: "角色分配"});
        this.handleGetEmployee();
    }

    componentWillUnmount() {
        this.setState = (count, callback) => {
            return;
        }
    }

    /**
     * 根据条件获取不同负责人列表
     */
    handleGetEmployee() {
        app.post(roleAssignment.SEARCH_DUTYEMPLOYEE, {duty_type: '0'}).then((result) => {
            if (result.code === 1) {
                let data = this.state.employeeTypeItem;
                data[0].dutyEmployee = result.data.dataSet;
                this.setState({dutyEmployee1: data});
            } else {
                //app.alert('接单负责人'+result.msg,'error',6);
                let data = this.state.employeeTypeItem;
                data[0].dutyEmployee = [];
                this.setState({dutyEmployee1: data});
            }
        });
        app.post(roleAssignment.SEARCH_DUTYEMPLOYEE, {duty_type: '1'}).then((result) => {
            if (result.code === 1) {
                let data = this.state.employeeTypeItem;
                data[1].dutyEmployee = result.data.dataSet;
                this.setState({dutyEmployee1: data});
            } else {
                //app.alert('配货负责人'+result.msg,'error',4);
                let data = this.state.employeeTypeItem;
                data[1].dutyEmployee = [];
                this.setState({dutyEmployee1: data});
            }
        });
        app.post(roleAssignment.SEARCH_DUTYEMPLOYEE, {duty_type: '2'}).then((result) => {
            if (result.code === 1) {
                let data = this.state.employeeTypeItem;
                data[2].dutyEmployee = result.data.dataSet;
                this.setState({dutyEmployee1: data});
            } else {
                // app.alert('对单负责人'+result.msg,'error',2);
                let data = this.state.employeeTypeItem;
                data[2].dutyEmployee = [];
                this.setState({dutyEmployee1: data});
            }
        });
        app.post(roleAssignment.SEARCH_DUTYEMPLOYEE, {duty_type: '3'}).then((result) => {
            if (result.code === 1) {
                let data = this.state.employeeTypeItem;
                data[3].dutyEmployee = result.data.dataSet;
                this.setState({dutyEmployee1: data});
            } else {
                //app.alert('配送负责人'+result.msg,'error',1);
                let data = this.state.employeeTypeItem;
                data[3].dutyEmployee = [];
                this.setState({dutyEmployee1: data});
            }
        });
        app.post(roleAssignment.SEARCH_DUTYEMPLOYEE, {duty_type: '4'}).then((result) => {
            if (result.code === 1) {
                let data = this.state.employeeTypeItem;
                data[4].dutyEmployee = result.data.dataSet;
                this.setState({dutyEmployee1: data});
            } else {
                //app.alert('配送负责人'+result.msg,'error',1);
                let data = this.state.employeeTypeItem;
                data[4].dutyEmployee = [];
                this.setState({dutyEmployee1: data});
            }
        });
    }

    /**
     * 处理刷新操作事件
     */
    handleRefresh() {
        const _this = this;
        app.post(roleAssignment.UPDATE_ROLE, {}).then((result) => {
            if (result.code === 1) {
                app.alert(result.msg, 'success', 3);
                _this.handleGetEmployee();
            } else {
                app.alert(result.msg, 'error', 3);
            }
        })
    };
};
export default prop => (
    <Consumer>
        {
            props => (
                <CustomerManage {...prop} {...props}/>
            )
        }
    </Consumer>
);